import router from "../router";
import type { RouteMeta, RouteRecordRaw } from "vue-router";
import { ro } from "element-plus/es/locale";
type Myrouter = RouteRecordRaw & { id?: string }
const allrouter: Myrouter[] = [
    {
        id: "1",
        name: "/",
        path: "",
        redirect: "/sheet/Home"
    },
    {
        id: "1",
        name: "Home",
        path: "Home",
        component: () => import("../view/Admn/Home/Home.vue")
    },
    {
        id: "2-1",
        name: "goodslist",
        path: "goodslist",
        component: () => import("../view/Admn/goods/goodslist.vue")
    },
    {
        id: "2-2",
        name: "searchgoods",
        path: "searchgoods",
        component: () => import("../view/Admn/goods/searchgood.vue")
    },
    
    {
        id: "3-1",
        name: "list",
        path: "list",
        component: () => import("../view/Admn/banner/swiper.vue")
    },
    {
        id: "3-2",
        name: "add",
        path: "add",
        component: () => import("../view/Admn/banner/addswiper.vue")
    },
    {
        id: "4-1",
        name: "adminlist",
        path: "adminlist",
        component: () => import("../view/Admn/administrator/adminList.vue")
    },
    {
        id: "4-2",
        name: "addadmin",
        path: "addadmin",
        component: () => import("../view/Admn/administrator/addAdmin.vue")
    },
    {
        id: "5-1",
        name: "shopping",
        path: "shopping",
        component: () => import("../view/Admn/shopping/shoppinglist.vue")
    },
    {
        id: "6-1",
        name: "userlist",
        path: "userlist",
        component: () => import("../view/Admn/Userlist/list.vue")
    },
    {
        id: "7-1",
        name: "Order",
        path: "Order",
        component: () => import("../view/Admn/Order/OrderList.vue")
    },
    {
        id: "8-1",
        name: "ChartKLine",
        path: "ChartKLine",
        component: () => import("../view/Admn/Chart/Chartdata.vue")
    },
    {
        id: "8-2",
        name: "ChartData",
        path: "ChartData",
        component: () => import("../view/Admn/Chart/KLine.vue")
    },
]
export default function getRoutes(){

    let  checkedKeys: string[] = []
    const arr = sessionStorage.getItem("grants")
    if (arr) {
        checkedKeys = JSON.parse(arr)
    }
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '2-3', '3', '3-1', '3-2', '4', '4-1', '4-2','5','5-1','6','6-1','7','7-1','8','8-1','8-2'] 
    }
    allrouter.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            let currentRoute = { ...item }

            // crud - 对应四位数
            // 先定义一个数组
            let arr = ["0", "0", "0", "0"]
            // 判断item.id中是否包含-C/-R/-U/-D
            if (checkedKeys.includes(item.id + "-C")) {
                arr[0] = "1";
            }
            if (checkedKeys.includes(item.id + "-R")) {
                arr[1] = "1";
            }
            if (checkedKeys.includes(item.id + "-U")) {
                arr[2] = "1";
            }
            if (checkedKeys.includes(item.id + "-D")) {
                arr[3] = "1";
            }

            currentRoute.meta = {
                crud: arr.join("")
            }
            router.addRoute("sheet", currentRoute)
           
          
        }
    })
}
export const clearCrudRoute = () => {
    let checkedKeys: string[] = []
    const power = sessionStorage.getItem("grants")
    if (power) {
        checkedKeys=JSON.parse(power)
    }
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '2-3', '3', '3-1', '3-2', '4', '4-1', '4-2','5','5-1','6','6-1','7','7-1','8','8-1','8-2']
    }
    allrouter.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            router.removeRoute(item.name as string)
        }
})
}

